//
//  BKScrollPageView.m
//  bestkeep
//
//  Created by utouu666 on 17/2/16.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKScrollPageView.h"

@interface BKScrollPageView()

@property (strong, nonatomic) BKSegmentStyle *segmentStyle;
@property (weak, nonatomic) BKScrollSegmentView *segmentView;
@property (weak, nonatomic) BKContentView *contentView;

@property (weak, nonatomic) UIViewController *parentViewController;
@property (strong, nonatomic) NSArray *childVcs;
@property (strong, nonatomic) NSArray *titlesArray;
@property(nonatomic,strong)NSArray *pictures;
@property(nonatomic,strong)NSArray *select_pictures;
@end

@implementation BKScrollPageView


- (instancetype)initWithFrame:(CGRect)frame segmentStyle:(BKSegmentStyle *)segmentStyle titles:(NSArray<NSString *> *)titles pictures:(NSArray *)pictures select_pictures:(NSArray *)select_pictures  parentViewController:(UIViewController *)parentViewController delegate:(id<BKScrollPageViewDelegate>) delegate {
    if (self = [super initWithFrame:frame]) {
        self.segmentStyle = segmentStyle;
        self.delegate = delegate;
        self.parentViewController = parentViewController;
        self.titlesArray = titles.copy;
        self.pictures = pictures.copy;
        self.select_pictures = select_pictures;
        [self commonInit];
    }
    return self;
}


- (void)commonInit {
    
    // 触发懒加载
    self.segmentView.backgroundColor = [UIColor whiteColor];
    self.contentView.backgroundColor = [UIColor whiteColor];
}

- (void)dealloc {
    NSLog(@"ZJScrollPageView--销毁");
}

#pragma mark - public helper

/** 给外界设置选中的下标的方法 */
- (void)setSelectedIndex:(NSInteger)selectedIndex animated:(BOOL)animated {
    [self.segmentView setSelectedIndex:selectedIndex animated:animated];
}

/**  给外界重新设置视图内容的标题的方法 */
- (void)reloadWithNewTitles:(NSArray<NSString *> *)newTitles {
    
    self.titlesArray = nil;
    self.titlesArray = newTitles.copy;
    
    [self.segmentView reloadTitlesWithNewTitles:self.titlesArray ];
    [self.contentView reload];
}


#pragma mark - getter ---- setter

- (BKContentView *)contentView {
    if (!_contentView) {
        BKContentView *content = [[BKContentView alloc] initWithFrame:CGRectMake(0.0, CGRectGetMaxY(self.segmentView.frame), self.bounds.size.width, self.bounds.size.height - CGRectGetMaxY(self.segmentView.frame)) segmentView:self.segmentView parentViewController:self.parentViewController delegate:self.delegate];
        [self addSubview:content];
        _contentView = content;
    }
    
    return  _contentView;
}


- (BKScrollSegmentView *)segmentView {
    if (!_segmentView) {
        __weak typeof(self) weakSelf = self;
        BKScrollSegmentView *segment = [[BKScrollSegmentView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.segmentStyle.segmentHeight) segmentStyle:self.segmentStyle delegate:self.delegate titles:self.titlesArray pictures:self.pictures select_pictures:self.select_pictures titleDidClick:^(BKTitleView *titleView, NSInteger index) {
            
            [weakSelf.contentView setContentOffSet:CGPointMake(weakSelf.contentView.bounds.size.width * index, 0.0) animated:weakSelf.segmentStyle.isAnimatedContentViewWhenTitleClicked];
            
        }];
        [self addSubview:segment];
        _segmentView = segment;
    }
    return _segmentView;
}


- (NSArray *)childVcs {
    if (!_childVcs) {
        _childVcs = [NSArray array];
    }
    return _childVcs;
}

- (NSArray *)titlesArray {
    if (!_titlesArray) {
        _titlesArray = [NSArray array];
    }
    return _titlesArray;
}

- (void)setExtraBtnOnClick:(ExtraBtnOnClick)extraBtnOnClick {
    _extraBtnOnClick = extraBtnOnClick;
    self.segmentView.extraBtnOnClick = extraBtnOnClick;
}


@end
